Skip to content

Lazy-init toolbox MCP connections on first request#46525

Merged
tendau merged 1 commit into
mainfrom
fix/lazy-toolbox-init
Apr 24, 2026
Merged

Lazy-init toolbox MCP connections on first request#46525
tendau merged 1 commit into
mainfrom
fix/lazy-toolbox-init

Conversation

@tendau

@tendau tendau commented Apr 24, 2026

Copy link
Copy Markdown
Member

Move toolbox MCP connect_toolboxes() from initialize() (startup) to _get_or_create_session() (first request) to fix race condition where tool discovery fails because platform-injected user context is not yet available at startup time.

Uses double-checked locking with asyncio.Lock to prevent concurrent initialization from parallel requests.

Description

Please add an informative description that covers that changes made by the pull request and link all relevant issues.

If an SDK is being regenerated based on a new API spec, a link to the pull request containing these API spec changes should be included above.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

Move toolbox MCP connect_toolboxes() from initialize() (startup) to
_get_or_create_session() (first request) to fix race condition where
tool discovery fails because platform-injected user context is not yet
available at startup time.

Uses double-checked locking with asyncio.Lock to prevent concurrent
initialization from parallel requests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 24, 2026 18:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Defers MCP toolbox connections from startup initialization to the first request/session creation to avoid missing platform-injected user context, using an asyncio.Lock to avoid concurrent initialization.

Changes:

  • Added lazy toolbox initialization (_ensure_toolboxes) guarded by double-checked locking.
  • Removed toolbox connection from initialize() and invoked it from _get_or_create_session().
  • Added state/lock fields to track one-time toolbox initialization.

@github-actions github-actions Bot added the Hosted Agents sdk/agentserver/* label Apr 24, 2026
@tendau tendau merged commit 0ebd192 into main Apr 24, 2026
27 checks passed
@tendau tendau deleted the fix/lazy-toolbox-init branch April 24, 2026 19:03
fafhrd91 pushed a commit to fafhrd91/azure-sdk-for-python that referenced this pull request Apr 28, 2026
Move toolbox MCP connect_toolboxes() from initialize() (startup) to
_get_or_create_session() (first request) to fix race condition where
tool discovery fails because platform-injected user context is not yet
available at startup time.

Uses double-checked locking with asyncio.Lock to prevent concurrent
initialization from parallel requests.

Co-authored-by: root <root@CPC-cearl-W9ZSG.localdomain>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hosted Agents sdk/agentserver/*

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants